package com.studentSystem.controller;

import com.studentSystem.pojo.Course;
import com.studentSystem.service.CourseService;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@Validated
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/list")
    public ResultUtil<PageBeanUtil<Course>> getAllCourse(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long teacherId
    ) {
        PageBeanUtil<Course> pb = courseService.getAllCourse(pageNum, pageSize, id, name, teacherId);
        return ResultUtil.success(pb);
    }

    @DeleteMapping("/delete")
    public ResultUtil deleteCourseById(Long id) {
        courseService.delete(id);
        return ResultUtil.success();
    }


    @PutMapping("/add")
    public ResultUtil addCourse(@RequestBody Course course) {
        courseService.add(course);
        return ResultUtil.success();
    }

    @PutMapping("/update")
    public ResultUtil updateCourse(@RequestBody Course course) {
        courseService.update(course);
        return ResultUtil.success();
    }

    @GetMapping("/get")
    public ResultUtil<Course> getCourseById(Long id) {
        Course course = courseService.getById(id);
        return ResultUtil.success(course);
    }

    @GetMapping("/all")
    public ResultUtil<List<Course>> getAllCoursses(){
        List<Course> courses = courseService.getAllCourses();
        return ResultUtil.success(courses);
    }
}
